package com.codeyang.controller.system;

import com.codeyang.domain.DictData;
import com.codeyang.dto.DictDataDto;
import com.codeyang.service.DictDataService;
import com.codeyang.utils.AjaxResult;
import com.codeyang.utils.ShiroSecurityUtils;
import com.codeyang.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 描述:
 *
 * @author CodeYang_Site
 * @version 2021/4/21 15:49
 */
@RestController
@RequestMapping("/system/dict/data")
public class DictDataController {

    @Autowired
    private DictDataService dictDataService;


    /**
     * 根据类型查询字典数据
     * 比如  根据 sys_normal_disable
     *
     * @param dictType 类型的名称
     * @return
     */
    @GetMapping("/getDataByType/{type}")
    public AjaxResult getDataByType(@PathVariable("type") String dictType) {

        List<DictData> dictDatas = dictDataService.getDataByType(dictType);
        return AjaxResult.success(dictDatas);

    }

    /**
     * 字典分页显示
     *
     * @param dictDataDto
     * @return
     */
    @GetMapping("/listForPage")
    public AjaxResult listForPage(DictDataDto dictDataDto) {
        DataGridView dataGridView = dictDataService.listForPage(dictDataDto);
        // mysql-db -- 数据持久化时间
        // HttpMessageConvert-->AbstractJackson2HttpMessageConverter->Jackson->ObjectMapper-> 日期修改为特定的类型
        return AjaxResult.success("success", dataGridView.getData(), dataGridView.getTotal());
    }


    /**
     * 新增
     *
     * @param dictDataDto
     * @return
     */
    @PostMapping("/addDictData")
    public AjaxResult addDictData(DictDataDto dictDataDto) {
        dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());

        boolean isSaveOk = dictDataService.saveDictData(dictDataDto);
        return isSaveOk ? AjaxResult.success() : AjaxResult.fail("新增失败");
    }

    /**
     * 修改字典数据
     *
     * @param dictDataDto
     * @return
     */
    @PutMapping("/updateDictData")
    public AjaxResult updateDictType(@Validated DictDataDto dictDataDto) {
        dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        boolean flage = dictDataService.updateDictData(dictDataDto);
        return flage ? AjaxResult.success() : AjaxResult.fail("修改失败");

    }

    /**
     * 删除字典-by ids
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteDictDataByIds/{ids}")
    public AjaxResult deleteDictDataByIds(@PathVariable("ids") Long[] ids) {
        boolean isDeleteOk = dictDataService.removeByIds(Arrays.asList(ids));
        return isDeleteOk ? AjaxResult.success() : AjaxResult.fail("删除失败");
    }

    @GetMapping("/getOne/{id}")
    public AjaxResult getOne(@PathVariable("id") Long id) {
        return AjaxResult.success(dictDataService.getById(id));
    }


}
